package com.ssy.lingxi.order.model.constant;

import com.ssy.lingxi.common.language.LanguageHolder;
import com.ssy.lingxi.message.api.constant.MessageTemplateCode;
import com.ssy.lingxi.order.model.vo.basic.response.DropdownItem;
import com.ssy.lingxi.report.api.enums.SaleOrderOperateTypeEnum;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 供应商内部状态枚举
 * @author 万宁
 * @version 2.0.0
 * @date 2021-07-15
 */
public enum VendorInnerStatusEnum {
    /**
     * 供应商订单初始状态，查询时要屏蔽此状态
     */
    WAITING_FOR_BUYER_SUBMIT(100, "等待采购商提交订单", 0, ""),

    /**
     * 销售订单：待提交审核 - 101
     */
    VENDOR_TO_SUBMIT_VALIDATE(101, "待提交审核", SaleOrderOperateTypeEnum.TO_BE_VALIFY_COMMIT.getCode(), MessageTemplateCode.sell_order_wait_audit),

    /**
     * 销售订单：提交审核不通过 - 102
     */
    VENDOR_SUBMIT_VALIDATE_NOT_PASSED(102, "提交审核不通过", 0, ""),

    /**
     * 销售订单：待审核(一级) - 103
     */
    VENDOR_VALIDATE_GRADE_ONE(103, "待审核(一级)", SaleOrderOperateTypeEnum.TO_BE_VALIFY_STEP1.getCode(), MessageTemplateCode.sell_order_audit_step_1),

    /**
     * 销售订单：审核不通过(一级) - 104
     */
    VENDOR_VALIDATE_GRADE_ONE_NOT_PASSED(104, "审核不通过(一级)", 0, ""),

    /**
     * 销售订单：待审核(二级) - 105
     */
    VENDOR_VALIDATE_GRADE_TWO(105, "待审核(二级)", SaleOrderOperateTypeEnum.TO_BE_VALIFY_STEP2.getCode(), MessageTemplateCode.sell_order_audit_step_2),

    /**
     * 销售订单：审核不通过(二级) - 106
     */
    VENDOR_VALIDATE_GRADE_TWO_NOT_PASSED(106, "审核不通过(二级)", 0, ""),

    /**
     * 销售订单：待确认 - 107
     */
    VENDOR_TO_CONFIRM(107, "待确认", SaleOrderOperateTypeEnum.TO_BE_CONFIRM.getCode(), MessageTemplateCode.sell_order_confirm),

    /**
     * 销售订单：已确认 - 108
     */
    VENDOR_CONFIRMED(108, "已确认", 0, ""),

    /**
     * 销售订单：待确认支付结果 - 109
     */
    VENDOR_TO_CONFIRM_PAYMENT(109, "待确认支付结果", SaleOrderOperateTypeEnum.TO_BE_CONFIRM_PAY_RESULT.getCode(), MessageTemplateCode.sell_order_confirm_payment),

    /**
     * 销售订单：已确认支付结果 - 110
     */
    VENDOR_PAYMENT_CONFIRMED(110, "已确认支付结果", 0, ""),

    /**
     * 销售订单：待新增销售发货单 - 111
     */
    VENDOR_TO_DELIVER(111, "待新增销售发货单", 0, MessageTemplateCode.sell_order_shipment),

    /**
     * 销售订单：待审核销售发货单 - 112
     */
    VENDOR_TO_VALIDATE_DELIVERY(112, "待审核销售发货单", SaleOrderOperateTypeEnum.TO_BE_ADD_DELIVERY_GOODS.getCode(), ""),

    /**
     * 销售订单：已审核销售发货单 - 113
     */
    VENDOR_DELIVERY_VALIDATE_PASSED(113, "已审核销售发货单", 0, ""),

    /**
     * 销售订单：待新增物流单 - 114
     */
    VENDOR_TO_LOGISTICS(114, "待新增物流单", 0, MessageTemplateCode.sell_order_logistics),

    /**
     * 销售订单：待确认物流单 - 115
     */
    VENDOR_TO_CONFIRM_LOGISTICS(115, "待确认物流单", SaleOrderOperateTypeEnum.TO_BE_ADD_LOGISTICS.getCode(), ""),

    /**
     * 销售订单：不接受物流单 - 116
     */
    VENDOR_LOGISTICS_NOT_PASSED(116, "不接受物流单", 0, ""),

    /**
     * 销售订单：已确认物流单 - 117
     */
    VENDOR_LOGISTICS_PASSED(117, "已确认物流单", 0, ""),

    /**
     * 销售订单：待确认发货 - 118
     */
    VENDOR_TO_CONFIRM_DELIVERY(118, "待确认发货", SaleOrderOperateTypeEnum.TO_BE_CONFIRM_DELIVERY_GOODS.getCode(), MessageTemplateCode.sell_order_shipment_confirm),

    /**
     * 销售订单：已确认发货 - 119
     */
    VENDOR_DELIVERY_CONFIRMED(119, "已确认发货", 0, ""),

    /**
     * 销售订单：待确认回单 - 120
     */
    VENDOR_TO_CONFIRM_RECEIPT(120, "待确认回单", SaleOrderOperateTypeEnum.TO_BE_CONFIRM_RECEIPT.getCode(), MessageTemplateCode.sell_order_receipt),

    /**
     * 销售订单：已确认回单 - 121
     */
    VENDOR_RECEIPT_CONFIRMED(121, "已确认回单", 0, ""),

    /**
     * 销售订单：待归档 - 122
     */
    VENDOR_TO_ARCHIVE(122, "待归档", SaleOrderOperateTypeEnum.TO_BE_ARCHIVE.getCode(), MessageTemplateCode.sell_order_archive),

    /**
     * 销售订单：自提订单已核销 - 123
     */
    VENDOR_TO_SELF_ORDER_VERIFY(123, "自提订单已核销", 0, ""),

    /*  ***************************************
     *  ******** (供采双方共同)内部状态 *********
     *  ***************************************
     */

    /**
     * 订单：已完成 - 1000
     */
    ACCOMPLISHED(1000, "已完成", 0, ""),

    /**
     * 订单：已取消 - 1001
     */
    CANCELLED(1001, "已取消", 0, ""),

    /**
     * 订单：已中止 - 1002
     */
    TERMINATED(1002, "已中止", 0, "");


    VendorInnerStatusEnum(Integer code, String name, Integer reportTypeCode, String messageCode) {
        this.code = code;
        this.reportTypeCode = reportTypeCode;
        this.messageCode = messageCode;
    }

    /**
     * 状态值
     */
    private Integer code;

    /**
     * 向报表服务发送操作的类型，对应 SaleOrderOperateTypeEnum
     */
    private Integer reportTypeCode;

    /**
     * 向消息服务发送数据请求的code，对应 MessageTemplateCode
     */
    private String messageCode;

    public Integer getCode() {
        return code;
    }

    public String getName() {
        return LanguageHolder.getName(this.getClass(), this.code);
    }

    public Integer getReportTypeCode() {
        return reportTypeCode;
    }

    public String getMessageCode() {
        return messageCode;
    }

    /**
     * 根据枚举值获得状态名称
     * @param code 枚举值
     * @return 状态名称
     */
    public static String getNameByCode(Integer code) {
        return LanguageHolder.getName(VendorInnerStatusEnum.class, code);
    }

    /**
     * 采购商内部状态列表
     * @return 前端下拉框状态列表
     */
    public static List<DropdownItem> toDropdownList() {
        String key = VendorInnerStatusEnum.class.getSimpleName().concat(".");
        return Arrays.stream(VendorInnerStatusEnum.values()).map(e -> new DropdownItem(e.getCode(), LanguageHolder.getName(key.concat(String.valueOf(e.getCode()))))).collect(Collectors.toList());
    }

    /**
     * 根据枚举值转换为枚举
     * @param code 枚举值
     * @return 枚举
     */
    public static VendorInnerStatusEnum parse(Integer code) {
        return code == null ? null : Arrays.stream(VendorInnerStatusEnum.values()).filter(e -> e.getCode().equals(code)).findFirst().orElse(null);
    }

    /**
     * 根据内部状态枚举值，获得报表服务的操作code
     * @param code 内部状态枚举
     * @return 报表服务定义的 SaleOrderOperateTypeEnum
     */
    public static Integer getReportCode(Integer code) {
        VendorInnerStatusEnum statusEnum = Arrays.stream(VendorInnerStatusEnum.values()).filter(e -> e.getCode().equals(code)).findFirst().orElse(null);
        return statusEnum == null ? 0 : statusEnum.getReportTypeCode();
    }

    /**
     * 根据内部状态枚举值，获得消息服务的操作code
     * @param code 内部状态枚举
     * @return 消息服务定义的 MessageTemplateCode
     */
    public static String getMessageCode(Integer code) {
        VendorInnerStatusEnum statusEnum = Arrays.stream(VendorInnerStatusEnum.values()).filter(e -> e.getCode().equals(code)).findFirst().orElse(null);
        return statusEnum == null ? "" : statusEnum.getMessageCode();
    }
}
